#include "LD3320_Demo.h"
#include "stdio.h"

extern UBYTE nAsrStatus;	
extern UDOUBLE nMp3Size;
extern UBYTE bMp3Play;	
extern UBYTE nLD_Mode;
extern volatile UBYTE flag_irq;
extern FILE *fp;

/********************************************************************************
function:	
				LD3320 INIT 
********************************************************************************/
void Ld3320_Init(){
	DEV_ModuleInit();
	Driver_Delay_ms(100);
	LD_init();
}

/********************************************************************************
function:	
				LD3320 ASR 
********************************************************************************/
int Ld3320_ASR(){
	UBYTE nAsrRes = 0;
	switch(nAsrStatus) {
		case LD_ASR_RUNING:
		case LD_ASR_ERROR:		
			return 0;
		case LD_ASR_NONE:
			nAsrStatus=LD_ASR_RUNING;
			if (LD_ASR()==0)//Start the ASR process once	
				nAsrStatus = LD_ASR_ERROR;
			return 0;
		case LD_ASR_FOUNDOK:
			nAsrRes = LD_GetResult();//once ASR process end, get the result									 
			// printf("Heading Code:%d\r\n", nAsrRes);			 										
			switch(nAsrRes) {//show the commond
				case CODE_SMS:					
					printf("LD3320_Demo:Ld3320_ASR: Send a text for help\r\n"); 
					break;
				case CODE_CALL:	 			
					printf("LD3320_Demo:Ld3320_ASR: Emergency contact is being called\r\n"); 
					break;							
				default:
					break;
			}	
			nAsrStatus = LD_ASR_NONE;
			return nAsrRes;
		case LD_ASR_FOUNDZERO:
		default:
			nAsrStatus = LD_ASR_NONE;
			return 0;
	}	
}

/********************************************************************************
function:	
				Play_mp3
********************************************************************************/
void Play_mp3(const char *path){
	//printf("file_path:%s\n",path);
	if((fp = fopen(path, "r"))  == NULL) {//open file
		printf("LD3320_Demo:Play_mp3: open failed\r\n");
		return;
	}
	
	UBYTE current_position = ftell(fp);			
	fseek(fp, 0, SEEK_END);
	nMp3Size = ftell(fp);//get file size
	fseek(fp, current_position, SEEK_SET);

	bMp3Play = 1;//playing status
	LD_Init_MP3();
	printf("LD3320_Demo:Play_mp3: LD_Init_MP3 finsh\n");
	LD_Adjust_Volume(1);						//adjust volume
	printf("LD3320_Demo:Play_mp3: LD_Adjust_Volume finsh\n");
	LD_play();									//start play
	printf("LD3320_Demo:Play_mp3: LD_play finsh\n");
}

